DataGridView中添加CheckBox(与DataSource分开添加) & C# winform单元格的formatted值的类型错误异常解决 |
您所在的位置:网站首页 › winform datagridview checkbox › DataGridView中添加CheckBox(与DataSource分开添加) & C# winform单元格的formatted值的类型错误异常解决 |
1,编辑DataGridView,添加一列 CheckBox ,Name 赋值为 "select",ColumnType 设置为"dATAGridViewCheckBoxColumn",HeaderTest 可以设置为想要的列名,如下图:
2. Debag 显示窗体的时候,会报 “System.FormatException:单元格的Formatted值的类型错误.要替换此默认对话框,请处理DataError事件" 的异常,点击确定出现新的提示: 【DataGridView中发生一下异常: SystemArgumentException:为DataGridViewCheckBoxCell提供的值的类型错误. 在System.Windows.Forms.DataGridViewCheckBoxCell.set_EditiingCellFormattedValue(Object value) 在System.Windows.Forms.DataGridView.InitializeEditingCellValue (DataGridViewCessStyle&dataGridViewCellStyle,DataGridViewCell&dataGridViewCell) 要替换此默认对话框,请处理DataError事件.】
解决方式: 在DataGridView的DataError事件中添加下列语句即可解决: { if (dataGridView1.Rows[e.RowIndex].IsNewRow) return; }
相关知识: DataGridView中几个事件发生的顺序记录如下,以备查寻: 在AllowUserToAddRows=true时候的事件发生次序, Form.Show ---> CellFormattimng (如果出错,goto DataError),注意这里的NewRow没有触发DefaultValuesNeeded事件。 如果发生其他比如 RowEnter事件: (DefaultValuesNeeded) ---> RowEnter ---> CellClick (DefaultValuesNeeded) ---> RowsAdded ---> UserAddedRow DefaultValuesNeeded事件不会发生在 IsNewRow=true的row上。
*扩展 根据上述相关知识,也可以在Cell Formatting 事件中进行异常处理: if (this.dgvImport.Columns[e.ColumnIndex].Name == "select") { if (e.Value == null) { e.Value = false; } }
相关知识转载自—— https://blog.csdn.net/weixin_33699914/article/details/85776307 https://www.cnblogs.com/yelanggu/p/6972839.html
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |